{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "19c61383",
   "metadata": {},
   "source": [
    "---\n",
    "* 2023-10-23\n",
    "* 课程：Python语言\n",
    "* week07\n",
    "---"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9a1af405",
   "metadata": {},
   "source": [
    "# 1. 列表"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a0a1ddda",
   "metadata": {},
   "source": [
    "* 思考：如果把我们班同学的名字都存起来，你会怎么做？用变量name1、name2、name3....来存么？"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8af1da6a",
   "metadata": {},
   "source": [
    "## 1.1 体验列表"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "74e48d6b",
   "metadata": {},
   "outputs": [],
   "source": [
    "name1 = \"张三\"\n",
    "name2 = \"李华\"\n",
    "name3 = \"李鹏\"\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "8648c07d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'张三'"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "name1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "8010463d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'李华'"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "name2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "1145063b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'李鹏'"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "name3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "56f74bcd",
   "metadata": {},
   "outputs": [],
   "source": [
    "name = \"张三，李华，李鹏，李小明\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "d370024d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'张三，李华，李鹏，李小明'"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "name"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "d738b0dd",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'张三'"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "name[:2]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "be492530",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'李华'"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "name[3:5]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d13c45c5",
   "metadata": {},
   "source": [
    "* 综上，一种新的数据结构就此而生"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "c283758f",
   "metadata": {},
   "outputs": [],
   "source": [
    "name_list = [\"张三\",\"李华\",\"李鹏\",\"李小明\"]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "6b4f082a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['张三', '李华', '李鹏', '李小明']"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "name_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "092d962e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'张三'"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "name_list[0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "50f97576",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'李华'"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "name_list[1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "717f26a9",
   "metadata": {},
   "outputs": [],
   "source": [
    "name_学号 = [20030101001,20030101002,20030101003,20030101004]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "24fd52a8",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[20030101001, 20030101002, 20030101003, 20030101004]"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "name_学号"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "6b544596",
   "metadata": {},
   "outputs": [],
   "source": [
    "name_list[1] = \"李树人\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "5d2464c2",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['张三', '李树人', '李鹏', '李小明']"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "name_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "bcbabb95",
   "metadata": {},
   "outputs": [],
   "source": [
    "name_张三 = [\"张三\",20030101001,19,180,75,\"Jack\",[\"Python\",\"移动互联网\",\"UI设计\"]]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "8c158ec5",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['张三', 20030101001, 19, 180, 75, 'Jack', ['Python', '移动互联网', 'UI设计']]"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "name_张三"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "41af2953",
   "metadata": {},
   "source": [
    "* 虽然列表可以存放不同类型的数据，但是一般情况我们存放的数据都是同一种数据类型的\n",
    "* 官网原话： Lists might contain items of different types, but usually the items all have the same type."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "76268c10",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 允许重复的数据**(数据重复性)**\n",
    "网三_Python成绩 = [95,97,94,94,95,97]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "a84a6c21",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[95, 97, 94, 94, 95, 97]"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "网三_Python成绩"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a747a60d",
   "metadata": {},
   "source": [
    "## 1.2 创建列表\n",
    "\n",
    "> 1. 使用list()创建列表\n",
    "> 2. 直接使用[]创建列表,中间的元素用**英文的** , 隔开\n",
    "> 3. 创建一个空列表，方法也是上面两种"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "6fff108c",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[18, 19, 18, 20]\n",
      "<class 'list'>\n"
     ]
    }
   ],
   "source": [
    "# 1. 使用list()创建列表\n",
    "my_list1 = list((18,19,18,20))\n",
    "print(my_list1)\n",
    "print(type(my_list1))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "55b184fc",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[75, 80, 74, 75]\n",
      "<class 'list'>\n"
     ]
    }
   ],
   "source": [
    "# 2. 直接使用[]创建列表,中间的元素用英文的 , 隔开\n",
    "my_list2 = [75,80,74,75]\n",
    "print(my_list2)\n",
    "print(type(my_list2))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7e01707a",
   "metadata": {},
   "source": [
    "* 特殊小案例"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "449ac8b2",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['p', 'y', 't', 'h', 'o', 'n']\n"
     ]
    }
   ],
   "source": [
    "my_list3 = list(\"python\")\n",
    "print(my_list3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "id": "819fe4b0",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['H', 'e', 'l', 'l', 'o', ' ', 'L', 'i', 's', 't', '~']\n"
     ]
    }
   ],
   "source": [
    "my_list4 = list(\"Hello List~\")\n",
    "print(my_list4)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "id": "2adb34bc",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[]\n",
      "<class 'list'>\n"
     ]
    }
   ],
   "source": [
    "# 3. 创建空列表\n",
    "empty_list = list()\n",
    "print(empty_list)\n",
    "print(type(empty_list))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "36c099d4",
   "metadata": {},
   "source": [
    "## 1.3 查看列表的长度 ： len()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "id": "61d7fd90",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "11"
      ]
     },
     "execution_count": 34,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "len(my_list4)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3f31a3e4",
   "metadata": {},
   "source": [
    "## 1.4 列表的取值和切片\n",
    "\n",
    "* index：取值\n",
    "* slice：切片\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "id": "8d84e58f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['p', 'y', 't', 'h', 'o', 'n']"
      ]
     },
     "execution_count": 35,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "python_list = list(\"python\")\n",
    "python_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "id": "4809fd10",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'p'"
      ]
     },
     "execution_count": 36,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "python_list[0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "id": "c38410f9",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'n'"
      ]
     },
     "execution_count": 37,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "python_list[-1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "id": "276d2e26",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['o', 'n']"
      ]
     },
     "execution_count": 41,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "python_list[4:]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "15aa1dce",
   "metadata": {},
   "source": [
    "## 1.5  列表的一般方法\n",
    "\n",
    "* 笔记：数据如何处理是数据诞生之后非常重要的事情\n",
    "\n",
    "* 数据的基本操作流程：\n",
    "> 1. 增加 add\n",
    "> 2. 减少/删除 remove\n",
    "> 3. 查询 find\n",
    "> 4. 修改 update"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "id": "752261ce",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['张三', '李华', '李鹏', '李小明']"
      ]
     },
     "execution_count": 42,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "name_list = [\"张三\",\"李华\",\"李鹏\",\"李小明\"]\n",
    "name_list"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2270d3fe",
   "metadata": {},
   "source": [
    "### 1. 增加"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "id": "05bc6ad7",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on built-in function append:\n",
      "\n",
      "append(object, /) method of builtins.list instance\n",
      "    Append object to the end of the list.\n",
      "\n"
     ]
    }
   ],
   "source": [
    "\n",
    "help(name_list.append)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "id": "d0e45871",
   "metadata": {},
   "outputs": [],
   "source": [
    "name_list.append(\"李树人\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "id": "3014974b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['张三', '李华', '李鹏', '李小明', '李树人']"
      ]
     },
     "execution_count": 47,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "name_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "id": "e8c227a4",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on built-in function insert:\n",
      "\n",
      "insert(index, object, /) method of builtins.list instance\n",
      "    Insert object before index.\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(name_list.insert)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "id": "829753c5",
   "metadata": {},
   "outputs": [],
   "source": [
    "name_list.insert(2,\"Marry\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "id": "ac45ab54",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['张三', '李华', 'Marry', '李鹏', '李小明', '李树人']"
      ]
     },
     "execution_count": 50,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "name_list"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "def5a6b7",
   "metadata": {},
   "source": [
    "### 2. 删除"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "id": "7a7b75af",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on built-in function pop:\n",
      "\n",
      "pop(index=-1, /) method of builtins.list instance\n",
      "    Remove and return item at index (default last).\n",
      "    \n",
      "    Raises IndexError if list is empty or index is out of range.\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(name_list.pop)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "id": "619f56da",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'李树人'"
      ]
     },
     "execution_count": 52,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "name_list.pop()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "id": "6fcdefed",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['张三', '李华', 'Marry', '李鹏', '李小明']"
      ]
     },
     "execution_count": 53,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "name_list"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3a05fbf5",
   "metadata": {},
   "source": [
    "* pop()的第二种使用方法：指定某一位（index）删除\n",
    "* 理论上：只要让我知道你的位置，我就可以pop()掉你！\n",
    "> 1. 只要满足某种条件的位置，我们就可以用pop()删掉"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "id": "9f2ea67b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'李华'"
      ]
     },
     "execution_count": 54,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "name_list.pop(1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "id": "cbb911d4",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['张三', 'Marry', '李鹏', '李小明']"
      ]
     },
     "execution_count": 55,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "name_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 56,
   "id": "221b834c",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on built-in function remove:\n",
      "\n",
      "remove(value, /) method of builtins.list instance\n",
      "    Remove first occurrence of value.\n",
      "    \n",
      "    Raises ValueError if the value is not present.\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(name_list.remove)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e2d1d454",
   "metadata": {},
   "source": [
    "* 思考：我怎么知道‘李鹏’在数据列表里面呢？"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 57,
   "id": "f6a2d73c",
   "metadata": {},
   "outputs": [],
   "source": [
    "name_list.remove('李鹏')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 58,
   "id": "d91679af",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['张三', 'Marry', '李小明']"
      ]
     },
     "execution_count": 58,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "name_list"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1e39b659",
   "metadata": {},
   "source": [
    "# 课后作业：把列表的所有方法尝试学习"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "98d88388",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
